package com.example.login;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends AppCompatActivity {
    private EditText et_username,et_pwd;
    private SharedPreferences mSharedPreferences;
    private CheckBox checkbox;
    private boolean is_login;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        et_username=findViewById(R.id.et_username);
        et_pwd=findViewById(R.id.et_pwd);
        checkbox=findViewById(R.id.checkbox);

        mSharedPreferences=getSharedPreferences("user",MODE_PRIVATE);
        is_login=mSharedPreferences.getBoolean("is_login",false);
        if (is_login) {
            String username = mSharedPreferences.getString("username", null);
            String password = mSharedPreferences.getString("password", null);
            et_username.setText(username);
            et_pwd.setText(password);
            checkbox.setChecked(true);
        }
        findViewById(R.id.register).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
            }
        });
        findViewById(R.id.login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String username= et_username.getText().toString();
                String password= et_pwd.getText().toString();
                if(TextUtils.isEmpty(username)||TextUtils.isEmpty(password)){
                    Toast.makeText(LoginActivity.this, "请填写完整信息", Toast.LENGTH_SHORT).show();
                }else {
                    UserInfo login=UserDbHelper.getInstance(LoginActivity.this).login(username);
                    if (login!=null){
                        if (username.equals(login.getUsername()) && password.equals(login.getPassword())){
                            SharedPreferences.Editor edit=mSharedPreferences.edit();
                            edit.putBoolean("is_login",is_login);
                            edit.putString("username",username);
                            edit.putString("password",password);

                            edit.commit();
                            //保存用户名和密码
                            UserInfo.setUserInfo(login);


                            Intent intent=new Intent(LoginActivity.this,MainActivity.class);
                            startActivity(intent);
                            finish();
                    }else {
                            Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                        }
                    }else {
                        Toast.makeText(LoginActivity.this, "该账号未注册", Toast.LENGTH_SHORT).show();
                    }


                }
            }
        });
        checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton button, boolean isChecked) {
                is_login=isChecked;
            }
        });

        }
    }
